package com.google.demo.Activity;

import com.google.demo.R;
import com.google.demo.ScanQRCodeApp;
import com.google.demo.Util.Util;
import com.google.demo.model.UserInfo;
import com.google.demo.view.AnimValidation;
import com.google.demo.view.AnimationRelativeLayout;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SettingAccountActivity extends MainActivity implements
		OnClickListener {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getTextViewHeader().setText(getString(R.string.txt_setting_account));
		getmButton_left().setVisibility(View.GONE);
		getmButton_right().setVisibility(View.GONE);
		getLeft_view().setVisibility(View.GONE);
		getRight_view().setVisibility(View.GONE);
		LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout mLayout = (LinearLayout) mLayoutInflater.inflate(
				R.layout.configure_layout, null);
		getFrameLayoutMain().removeAllViews();
		getFrameLayoutMain().addView(mLayout);
		initControls(mLayout);
		initData();
	}

	private void initData() {
		UserInfo userInfo = app.getLoginInfo();
		if (userInfo.ismIsLogin()) {
			mTextView_accountName.setText(userInfo.getmUsername());
			mButton_connect.setText(getString(R.string.txt_disconnect));
			mButton_connect.setBackgroundResource(R.drawable.disconnect_btn);
		} else {
			mTextView_accountName.setText("");
			mButton_connect.setText(getString(R.string.txt_connect));
			mButton_connect.setBackgroundResource(R.drawable.login_btn);
		}
	}

	private Button mButton_connect;
	private TextView mTextView_changepass, mTextView_accountName;
	private AnimationRelativeLayout mRelative_changepass;
	private TextView mTextView_markCurrent,mTextView_markNew,mTextView_markConfirm;
	private EditText mEditText_markCurrent,mEditText_markNew,mEditText_markConfirm;
	ScanQRCodeApp app = ScanQRCodeApp.Instance();

	private void initControls(LinearLayout mLayout) {
		mButton_connect = (Button) mLayout.findViewById(R.id.btn_connect);
		mTextView_accountName = (TextView) mLayout
				.findViewById(R.id.txt_account_name);
		mTextView_changepass = (TextView) mLayout
				.findViewById(R.id.textView_changepassword);
		mRelative_changepass = (AnimationRelativeLayout) mLayout
				.findViewById(R.id.linearLayout1_changepass);
		mButton_connect.setOnClickListener(this);
		mTextView_changepass.setOnClickListener(this);
		mRelative_changepass.setOnClickListener(this);
		
		mTextView_markCurrent=(TextView) mRelative_changepass.findViewById(R.id.txtv_markCurrent_password);
		mTextView_markNew=(TextView) mRelative_changepass.findViewById(R.id.txtv_markNew_password);
		mTextView_markConfirm=(TextView) mRelative_changepass.findViewById(R.id.txtv_markConfirm_password);
		
		mEditText_markCurrent=(EditText) mRelative_changepass.findViewById(R.id.edit_current_password);
		mEditText_markNew=(EditText) mRelative_changepass.findViewById(R.id.edit_new_password);
		mEditText_markConfirm=(EditText) mRelative_changepass.findViewById(R.id.edit_confirm_password);
		
		AnimValidation validation=new AnimValidation(this, 1, mEditText_markCurrent, mEditText_markNew,mEditText_markConfirm,
				mTextView_markCurrent, mTextView_markNew, mTextView_markConfirm);
		validation.validAnim();
	}

	private void showAnimLayout() {
		if (mRelative_changepass.getVisibility() == View.GONE
				|| mRelative_changepass.getVisibility() == View.INVISIBLE) {
			mRelative_changepass.show(true, false);
		} else {
			mRelative_changepass.hide(true, true);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == mButton_connect) {
			if (mButton_connect.getText().equals(
					getString(R.string.txt_connect))) {
				Intent intent = new Intent(SettingAccountActivity.this,
						LoginMainActivity.class);				
				startActivityForResult(intent, 100);
				finish();
			} else {
				ScanQRCodeApp.Instance().isFirstStartLoginActivity=false;
				app.saveLogoutInfo();
				mButton_connect.setBackgroundResource(R.drawable.login_btn);
				mButton_connect.setText(getString(R.string.txt_connect));
				mTextView_accountName.setText("");
			
			}
		} else if (v == mTextView_changepass) {
			
			showAnimLayout();
			
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100
				&& resultCode == RESULT_OK) {
			Intent intent = new Intent(SettingAccountActivity.this,
					InputDataActivity.class);
			startActivity(intent);
			UIBottomButtonsHelper.CURRENT_SCREEN_OF_4_MAINSCREEN=UIBottomButtonsHelper.SCAN_SCREEN;
			finish();

		}else{
			
		}
	}
}
